package cn.tedu.egweibo2.controller;

import cn.tedu.egweibo2.mapper.CommentMapper;
import cn.tedu.egweibo2.pojo.dto.CommentDTO;
import cn.tedu.egweibo2.pojo.entity.Comment;
import cn.tedu.egweibo2.pojo.vo.CommentVO;
import cn.tedu.egweibo2.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/v1/comment")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;

    /**
     * 发布评论
     * 接口地址: /v1/comment/insert
     * 请求方式: POST
     * 请求数据类型: application/json
     * 响应数据类型: int
     */
    @PostMapping("/insert")
    public int insertComment(@RequestBody CommentDTO commentDTO, @ApiIgnore HttpSession session){
        //打桩
        log.debug(commentDTO.toString());
        //检验登录状态
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null)
            return 2;
        //发布评论
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO,comment);
        comment.setCreated(new Date());
        comment.setUserId(userVO.getId());
        //打桩
        log.debug(comment.toString());
        commentMapper.insertComment(comment);
        return 1;
    }

    /**
     * 根据微博id获取评论[列表]
     * 接口地址: /v1/comment/selectByWeiboId
     * 请求方式: GET
     * 请求数据类型: application/x-www-form-urlencoded
     * 响应数据类型: List<CommentVO>
     */
    @GetMapping("selectByWeiboId")
    public List<CommentVO> selectByWeiboId(Integer id){
        return commentMapper.selectByWeiboId(id);
    }
}
